﻿using Microsoft.Extensions.Logging;
using ST.Common.Util;
using ST.Common.Util.Text;
using ST.Common.Util.Xml;
using ST.WX.Common.Exceptions;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Text;
using System.Web;
using System.Xml;

namespace ST.WX.Common.Pay
{
	/// <summary>
	/// 订单查询返回数据
	/// </summary>
	public class WXPayOrderQueryRet : WXPayRet
	{
		/// <summary>
		/// 设备号
		/// </summary>
		[StringLength(32)]
		public string device_info { get; set; }

		/// <summary>
		/// 用户标识
		/// </summary>
		[StringLength(128)]
		public string openid { get; set; }

		/// <summary>
		/// 是否关注公众账号（Y-关注，N-未关注）
		/// </summary>
		public bool is_subscribe { get; set; }

		/// <summary>
		/// 交易类型（取值为：JSAPI，NATIVE，APP等）
		/// </summary>
		[StringLength(16)]
		public string trade_type { get; set; }

		/// <summary>
		/// 交易状态（SUCCESS—支付成功 REFUND—转入退款 NOTPAY—未支付 CLOSED—已关闭 REVOKED—已撤销（刷卡支付） USERPAYING--用户支付中 PAYERROR--支付失败(其他原因，如银行返回失败)）
		/// </summary>
		[StringLength(32)]
		public string trade_state { get; set; }

		/// <summary>
		/// 交易状态描述（对当前查询订单状态的描述和下一步操作的指引）
		/// </summary>
		[StringLength(256)]
		public string trade_state_desc { get; set; }

		/// <summary>
		/// 付款银行
		/// </summary>
		[StringLength(16)]
		public string bank_type { get; set; }

		/// <summary>
		/// 货币种类（符合ISO 4217标准的三位字母代码，默认人民币：CNY）
		/// </summary>
		[Display(Name = "货币种类")]
		[StringLength(16)]
		public string fee_type { get; set; }

		/// <summary>
		/// 订单金额（订单总金额，单位为分）
		/// </summary>
		[Display(Name = "订单金额")]
		public int total_fee { get; set; }

		/// <summary>
		/// 应结订单金额（单位：分，应结订单金额=订单金额-非充值代金券金额，应结订单金额<=订单金额）
		/// </summary>
		[Display(Name = "应结订单金额")]
		public int settlement_total_fee { get; set; }

		/// <summary>
		/// 现金支付金额（现金支付金额订单现金支付金额）
		/// </summary>
		[Display(Name = "现金支付金额")]
		public int cash_fee { get; set; }

		/// <summary>
		/// 现金支付货币类型（货币类型，符合ISO4217标准的三位字母代码，默认人民币：CNY）
		/// </summary>
		[StringLength(16)]
		public string cash_fee_type { get; set; }

		/// <summary>
		/// 总代金券金额（代金券金额<=订单金额，订单金额-代金券金额=现金支付金额）
		/// </summary>
		[Display(Name = "总代金券金额")]
		public int coupon_fee { get; set; }

		/// <summary>
		/// 代金券使用数量
		/// </summary>
		[Display(Name = "代金券使用数量")]
		public int coupon_count { get; set; }

		/// <summary>
		/// 微信支付订单号
		/// </summary>
		[Display(Name = "微信支付订单号")]
		[StringLength(32)]
		public string transaction_id { get; set; }

		/// <summary>
		/// 商户订单号
		/// </summary>
		[Display(Name = "商户订单号")]
		[StringLength(32)]
		public string out_trade_no { get; set; }

		/// <summary>
		/// 商家数据包
		/// </summary>
		[Display(Name = "商家数据包")]
		[StringLength(128)]
		public string attach { get; set; }

		/// <summary>
		/// 支付完成时间（格式为yyyyMMddHHmmss）
		/// </summary>
		public DateTime time_end { get; set; }

		protected override void ConvertToObj()
		{
			base.ConvertToObj();//需要调用基类解析基础返回字段
			if (values.ContainsKey("trade_type"))
			{
				trade_type = Convert.ToString(values["trade_type"]);
			}
			if (values.ContainsKey("trade_state"))
			{
				trade_state = Convert.ToString(values["trade_state"]);
			}
			if (values.ContainsKey("trade_state_desc"))
			{
				trade_state_desc = Convert.ToString(values["trade_state_desc"]);
			}
			if (values.ContainsKey("device_info"))
			{
				device_info = Convert.ToString(values["device_info"]);
			}
			if (values.ContainsKey("openid"))
			{
				openid = Convert.ToString(values["openid"]);
			}
			if (values.ContainsKey("is_subscribe"))
			{
				is_subscribe = Convert.ToString(values["is_subscribe"]) == "Y";
			}
			if (values.ContainsKey("bank_type"))
			{
				bank_type = Convert.ToString(values["bank_type"]);
			}
			if (values.ContainsKey("fee_type"))
			{
				fee_type = Convert.ToString(values["fee_type"]);
			}
			if (values.ContainsKey("total_fee"))
			{
				total_fee = Convert.ToInt32(values["total_fee"]);
			}
			if (values.ContainsKey("settlement_total_fee"))
			{
				settlement_total_fee = Convert.ToInt32(values["settlement_total_fee"]);
			}
			if (values.ContainsKey("cash_fee"))
			{
				cash_fee = Convert.ToInt32(values["cash_fee"]);
			}
			if (values.ContainsKey("cash_fee_type"))
			{
				cash_fee_type = Convert.ToString(values["cash_fee_type"]);
			}
			if (values.ContainsKey("coupon_fee"))
			{
				coupon_fee = Convert.ToInt32(values["coupon_fee"]);
			}
			if (values.ContainsKey("coupon_count"))
			{
				coupon_count = Convert.ToInt32(values["coupon_count"]);
			}
			if (values.ContainsKey("transaction_id"))
			{
				transaction_id = Convert.ToString(values["transaction_id"]);
			}
			if (values.ContainsKey("out_trade_no"))
			{
				out_trade_no = Convert.ToString(values["out_trade_no"]);
			}
			if (values.ContainsKey("attach"))
			{
				attach = Convert.ToString(values["attach"]);
			}
			if (values.ContainsKey("time_end"))
			{
				time_end = TextHelper.GetDateTime(Convert.ToString(values["time_end"]), "yyyyMMddHHmmss");
			}
		}

		public override void Validate()
		{
			base.Validate();//需要调用基类进行基本判断
							//if (result_code != "SUCCESS")
							//{
							//	throw new WXPayException("错误代码：" + err_code + " " + err_code_des);
							//}
		}
	}
}
